/*
 * Copyright (C) 2011 Samsung Electronics Corporation. All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided the following conditions
 * are met:
 * 
 * 1.  Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 * 
 * 2.  Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 * 
 * THIS SOFTWARE IS PROVIDED BY SAMSUNG ELECTRONICS CORPORATION AND ITS
 * CONTRIBUTORS "AS IS", AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING
 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SAMSUNG
 * ELECTRONICS CORPORATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES(INCLUDING
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS, OR BUSINESS INTERRUPTION), HOWEVER CAUSED AND ON ANY THEORY
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT(INCLUDING
 * NEGLIGENCE OR OTHERWISE ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "config.h"

#if ENABLE(WEBCL)

#include "WebCLException.h"

namespace WebCore {

// This should be an array of structs to pair the names and descriptions. ??
static const char* const exceptionNames[] = {
   "SUCCESS", 
	"DEVICE_NOT_FOUND",
	"DEVICE_NOT_AVAILABLE",
	"COMPILER_NOT_AVAILABLE",
	"MEM_OBJECT_ALLOCATION_FAILURE",
	"OUT_OF_RESOURCES",
	"OUT_OF_HOST_MEMORY",
	"PROFILING_INFO_NOT_AVAILABLE",
	"MEM_COPY_OVERLAP",
	"IMAGE_FORMAT_MISMATCH",
	"IMAGE_FORMAT_NOT_SUPPORTED",
	"BUILD_PROGRAM_FAILURE",
	"MAP_FAILURE",
	"MISALIGNED_SUB_BUFFER_OFFSET",
	"EXEC_STATUS_ERROR_FOR_EVENTS_IN_WAIT_LIST",
	"INVALID_VALUE",
	"INVALID_DEVICE_TYPE",
	"INVALID_PLATFORM",
	"INVALID_DEVICE",
	"INVALID_CONTEXT",
	"INVALID_QUEUE_PROPERTIES",
	"INVALID_COMMAND_QUEUE",
	"INVALID_HOST_PTR",
	"INVALID_MEM_OBJECT",
	"INVALID_IMAGE_FORMAT_DESCRIPTOR",
	"INVALID_IMAGE_SIZE",
	"INVALID_SAMPLER",
	"INVALID_BINARY",
	"INVALID_BUILD_OPTIONS",
	"INVALID_PROGRAM",
	"INVALID_PROGRAM_EXECUTABLE",
	"INVALID_KERNEL_NAME",
	"INVALID_KERNEL_DEFINITION",
	"INVALID_KERNEL",
	"INVALID_ARG_INDEX",
	"INVALID_ARG_VALUE",
	"INVALID_ARG_SIZE",
	"INVALID_KERNEL_ARGS",
	"INVALID_WORK_DIMENSION",
	"INVALID_WORK_GROUP_SIZE",
	"INVALID_WORK_ITEM_SIZE",
	"INVALID_GLOBAL_OFFSET",
	"INVALID_EVENT_WAIT_LIST",
	"INVALID_EVENT",
	"INVALID_OPERATION",
	"INVALID_GL_OBJECT",
	"INVALID_BUFFER_SIZE",
	"INVALID_MIP_LEVEL",
	"INVALID_GLOBAL_WORK_SIZE",
	"INVALID_PROPERTY",
	"FAILURE",
};
// Messages are not proper
static const char* const exceptionDescriptions[] = {
	"WEBCL_IMPL_SUCCESS",
	"WEBCL_IMPL_DEVICE_NOT_FOUND",
	"WEBCL_IMPL_DEVICE_NOT_AVAILABLE",
	"WEBCL_IMPL_COMPILER_NOT_AVAILABLE",
	"WEBCL_IMPL_MEM_OBJECT_ALLOCATION_FAILURE",
	"WEBCL_IMPL_OUT_OF_RESOURCES",
	"WEBCL_IMPL_OUT_OF_HOST_MEMORY",
	"WEBCL_IMPL_PROFILING_INFO_NOT_AVAILABLE",
	"WEBCL_IMPL_MEM_COPY_OVERLAP",
	"WEBCL_IMPL_IMAGE_FORMAT_MISMATCH",
	"WEBCL_IMPL_IMAGE_FORMAT_NOT_SUPPORTED",
	"WEBCL_IMPL_BUILD_PROGRAM_FAILURE",
	"WEBCL_IMPL_MAP_FAILURE",
	"WEBCL_IMPL_MISALIGNED_SUB_BUFFER_OFFSET",
	"WEBCL_IMPL_EXEC_STATUS_ERROR_FOR_EVENTS_IN_WAIT_LIST",
	"WEBCL_IMPL_INVALID_VALUE",
	"WEBCL_IMPL_INVALID_DEVICE_TYPE",
	"WEBCL_IMPL_INVALID_PLATFORM",
	"WEBCL_IMPL_INVALID_DEVICE",
	"WEBCL_IMPL_INVALID_CONTEXT",
	"WEBCL_IMPL_INVALID_QUEUE_PROPERTIES",
	"WEBCL_IMPL_INVALID_COMMAND_QUEUE",
	"WEBCL_IMPL_INVALID_HOST_PTR",
	"WEBCL_IMPL_INVALID_MEM_OBJECT",
	"WEBCL_IMPL_INVALID_IMAGE_FORMAT_DESCRIPTOR",
	"WEBCL_IMPL_INVALID_IMAGE_SIZE",
	"WEBCL_IMPL_INVALID_SAMPLER",
	"WEBCL_IMPL_INVALID_BINARY",
	"WEBCL_IMPL_INVALID_BUILD_OPTIONS",
	"WEBCL_IMPL_INVALID_PROGRAM",
	"WEBCL_IMPL_INVALID_PROGRAM_EXECUTABLE",
	"WEBCL_IMPL_INVALID_KERNEL_NAME",
	"WEBCL_IMPL_INVALID_KERNEL_DEFINITION",
	"WEBCL_IMPL_INVALID_KERNEL",
	"WEBCL_IMPL_INVALID_ARG_INDEX",
	"WEBCL_IMPL_INVALID_ARG_VALUE",
	"WEBCL_IMPL_INVALID_ARG_SIZE",
	"WEBCL_IMPL_INVALID_KERNEL_ARGS",
	"WEBCL_IMPL_INVALID_WORK_DIMENSION",
	"WEBCL_IMPL_INVALID_WORK_GROUP_SIZE",
	"WEBCL_IMPL_INVALID_WORK_ITEM_SIZE",
	"WEBCL_IMPL_INVALID_GLOBAL_OFFSET",
	"WEBCL_IMPL_INVALID_EVENT_WAIT_LIST",
	"WEBCL_IMPL_INVALID_EVENT",
	"WEBCL_IMPL_INVALID_OPERATION",
	"WEBCL_IMPL_INVALID_GL_OBJECT",
	"WEBCL_IMPL_INVALID_BUFFER_SIZE",
	"WEBCL_IMPL_INVALID_MIP_LEVEL",
	"WEBCL_IMPL_INVALID_GLOBAL_WORK_SIZE",
	"WEBCL_IMPL_INVALID_PROPERTY",
	"WEBCL_IMPL_FAILURE",
};

COMPILE_ASSERT(WTF_ARRAY_LENGTH(exceptionNames) == WTF_ARRAY_LENGTH(exceptionDescriptions), WebCLExceptionTablesMustMatch);

bool WebCLException::initializeDescription(ExceptionCode ec, ExceptionCodeDescription* description)
{
    if (ec < WebCLExceptionOffset || ec > WebCLExceptionMax)
        return false;

    description->typeName = "DOM WebCL";
	
	// As INVALID_VALUE=-30 No error code between -15 to -30
	if(ec < 15)
    	description->code =  WebCLExceptionOffset - ec;
	else
    	description->code =  WebCLExceptionOffset - ec - 15;
		
	// Change the type
	description->type = WebCLExceptionType;

    size_t tableSize = WTF_ARRAY_LENGTH(exceptionNames);
    size_t tableIndex = ec - SUCCESS;

    description->name = tableIndex < tableSize ? exceptionNames[tableIndex] : 0;
    description->description = tableIndex < tableSize ? exceptionDescriptions[tableIndex] : 0;

    return true;
}

} // namespace WebCore

#endif // ENABLE(WEBCL)
